package com.bt.lms.ui.page.sys.page.paging;

import com.bt.lms.entity.Major;
import com.bt.lms.entity.Role;
import com.bt.lms.entity.StudyCen;
import com.bt.lms.entity.User;
import com.bt.lms.entity.page.Page;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import com.bt.lms.ui.page.sys.style.LabStyle;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.MyHBox;
import com.bt.lms.ui.page.sys.utils.MyLabel;
import com.bt.lms.ui.page.sys.utils.NetWorkUtils;
import com.bt.lms.ui.vo.PageSkipInfo;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;


/**
 * 分页查询区
 * 界面组件较少，直接切换整个界面
 * 同时，自身表现为横向布局
 *
 * @author 徐大永
 */

public class PagePagingQuery<T> extends MyHBox {

    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 当前分页针对的对象
     */
    private final Page<T> page;

    /**
     * 当前分页对象的类型
     */
    private final Class<T> type;

    /**
     * 构建分页界面
     *
     * @param pageSkipInfo 界面跳转信息
     * @param page         界面数据
     */
    public PagePagingQuery(PageSkipInfo pageSkipInfo, Page<T> page, Class<T> type) {

        //维护
        this.pageSkipInfo = pageSkipInfo;
        this.page = page;
        this.type = type;

        //布局
        Insets insets = new Insets(10);
        this.setPadding(insets);
        this.setSpacing(10);
        this.setAlignment(Pos.CENTER_RIGHT);

        //后退标签
        retreatLabInit();
        //控制按钮
        commandBtnInit();
        //前进标签
        advanceLabInit();
    }

    /**
     * 前进标签初始化
     */
    private void advanceLabInit() {

        int width = 45;
        Long total = page.getTotal();//总条数
        int current = page.getCurrent();//当前页数
        int size = page.getSize();//每页条数
        int pageSum;

        if (total % size != 0)
            pageSum = (int) (total / size + 1);//总页数
        else
            pageSum = (int) (total / size);//总页数

        String presentStr = "/" + current;

        MyLabel nextLab = new MyLabel("下一页 »");
        this.

                getChildren().

                add(nextLab);
        //样式
        LabStyle.paging(nextLab);
        //事件
        LabStyle.pagingAction(nextLab);
        nextLab.setOnMouseClicked(event ->

        {
            if (current < pageSum) {
                Page<T> userPage = new Page<>();
                userPage.setCurrent(current + 1);
                adapter(userPage);
            }
        });

        MyLabel finallyLab = new MyLabel("尾页 »");
        this.

                getChildren().

                add(finallyLab);
        //样式
        LabStyle.paging(finallyLab);
        //事件
        LabStyle.pagingAction(finallyLab);
        finallyLab.setOnMouseClicked(event ->

        {
            Page<T> userPage = new Page<>();
            userPage.setCurrent(pageSum);
            adapter(userPage);
        });

        //条数统计
        pagingSum(this, total);

        //跳转框
        TextField pageNumberTF = new TextField();
        pageNumberTF.setPrefWidth(width);
        this.

                getChildren().

                add(pageNumberTF);

        //当前页标签
        Label presentLab = new Label(presentStr);
        this.

                getChildren().

                add(presentLab);

        //跳转按钮
        MyButton notarizeBtn = new MyButton("GO");
        this.

                getChildren().

                add(notarizeBtn);
        //事件
        notarizeBtn.setOnAction(event ->

        {
            String currentStr = pageNumberTF.getText();
            try {
                int i = Integer.parseInt(currentStr);
                //和总页数对比
                if (i <= pageSum && i > 0) {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(i);
                    adapter(userPage);
                }

            } catch (NumberFormatException ignored) {

            }

        });
    }

    /**
     * 控制按钮初始化
     */
    private void commandBtnInit() {

        //当前页
        Long total = page.getTotal();//总条数
        int current = page.getCurrent();//当前页数
        int size = page.getSize();//每页条数
        int pageSum = (int) (total / size + 1);//总页数

        //如果只有一页
        if (pageSum == 1) {
            MyButton pageOne = new MyButton(String.valueOf(current));
            this.getChildren().add(pageOne);
            //样式
            BtnStyle.pagingCurrent(pageOne);
            //事件
            BtnStyle.pagingCurrentAction(pageOne);
            pageOne.setOnAction(event -> {
                Page<T> userPage = new Page<>();
                userPage.setCurrent(current);
                adapter(userPage);
            });
        } else if (pageSum == 2) {//只有两页
            //判断当前页
            if (current == 1) {//如果是第一页

                MyButton pageOne = new MyButton(String.valueOf(current));
                this.getChildren().add(pageOne);
                //样式
                BtnStyle.pagingCurrent(pageOne);
                //事件
                BtnStyle.pagingCurrentAction(pageOne);
                pageOne.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current);
                    adapter(userPage);
                });

                MyButton pageTwo = new MyButton(String.valueOf(current + 1));
                this.getChildren().add(pageTwo);
                //样式
                BtnStyle.paging(pageTwo);
                //事件
                BtnStyle.pagingAction(pageTwo);
                pageTwo.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current + 1);
                    adapter(userPage);
                });
            } else if (current == 2) {//第二页
                //是否是最后一页
                MyButton pageOne = new MyButton(String.valueOf(current - 1));
                this.getChildren().add(pageOne);
                //样式
                BtnStyle.paging(pageOne);
                //事件
                BtnStyle.pagingAction(pageOne);
                pageOne.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current - 1);
                    adapter(userPage);
                });

                MyButton pageTwo = new MyButton(String.valueOf(current));
                this.getChildren().add(pageTwo);
                //样式
                BtnStyle.pagingCurrent(pageTwo);
                //事件
                BtnStyle.pagingCurrentAction(pageTwo);
                pageTwo.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current);
                    adapter(userPage);
                });
            }

        } else if (pageSum >= 3) {//页数在三页及以上
            if (current == 1) {//判断当前是否是第一页

                MyButton pageOne = new MyButton(String.valueOf(current));
                this.getChildren().add(pageOne);
                //样式
                BtnStyle.pagingCurrent(pageOne);
                //事件
                BtnStyle.pagingCurrentAction(pageOne);
                pageOne.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current);
                    adapter(userPage);
                });

                MyButton pageTwo = new MyButton(String.valueOf(current + 1));
                this.getChildren().add(pageTwo);
                //样式
                BtnStyle.paging(pageTwo);
                //事件
                BtnStyle.pagingAction(pageTwo);
                pageTwo.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current + 1);
                    adapter(userPage);
                });

                MyButton pageThree = new MyButton(String.valueOf(current + 2));
                this.getChildren().add(pageThree);
                //样式
                BtnStyle.paging(pageThree);
                //事件
                BtnStyle.pagingAction(pageThree);
                pageThree.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current + 2);
                    adapter(userPage);
                });
            } else if (current == pageSum) {//如果是最后一页
                MyButton pageOne = new MyButton(String.valueOf(current - 2));
                this.getChildren().add(pageOne);
                //样式
                BtnStyle.paging(pageOne);
                //事件
                BtnStyle.pagingAction(pageOne);
                pageOne.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current - 1);
                    adapter(userPage);
                });

                MyButton pageTwo = new MyButton(String.valueOf(current - 1));
                this.getChildren().add(pageTwo);
                //样式
                BtnStyle.paging(pageTwo);
                //事件
                BtnStyle.pagingAction(pageTwo);
                pageTwo.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current);
                    adapter(userPage);
                });

                MyButton pageThree = new MyButton(String.valueOf(current));
                this.getChildren().add(pageThree);
                //样式
                BtnStyle.pagingCurrent(pageThree);
                //事件
                BtnStyle.pagingCurrentAction(pageThree);
                pageThree.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current + 1);
                    adapter(userPage);
                });
            } else {//是中间页情况

                MyButton pageOne = new MyButton(String.valueOf(current - 1));
                this.getChildren().add(pageOne);
                //样式
                BtnStyle.paging(pageOne);
                //事件
                BtnStyle.pagingAction(pageOne);
                pageOne.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current - 1);
                    adapter(userPage);
                });

                MyButton pageTwo = new MyButton(String.valueOf(current));
                this.getChildren().add(pageTwo);
                //样式
                BtnStyle.pagingCurrent(pageTwo);
                //事件
                BtnStyle.pagingCurrentAction(pageTwo);
                pageTwo.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current);
                    adapter(userPage);
                });

                MyButton pageThree = new MyButton(String.valueOf(current + 1));
                this.getChildren().add(pageThree);
                //样式
                BtnStyle.paging(pageThree);
                //事件
                BtnStyle.pagingAction(pageThree);
                pageThree.setOnAction(event -> {
                    Page<T> userPage = new Page<>();
                    userPage.setCurrent(current + 1);
                    adapter(userPage);
                });
            }
        }

    }

    /**
     * 后退标签初始化
     */
    private void retreatLabInit() {

        Integer current = page.getCurrent();

        MyLabel firstLab = new MyLabel("« 首页");
        this.getChildren().add(firstLab);
        //样式
        LabStyle.paging(firstLab);
        //事件
        LabStyle.pagingAction(firstLab);
        firstLab.setOnMouseClicked(event -> adapter(new Page<>()));

        MyLabel lastLab = new MyLabel("« 上一页");
        this.getChildren().add(lastLab);
        //样式
        LabStyle.paging(lastLab);
        //事件
        LabStyle.pagingAction(lastLab);
        lastLab.setOnMouseClicked(event -> {
            Page<T> userPage = new Page<>();
            //判断，如果不是第一页就减一,如果是，就不做任何处理
            if (current != 1) {
                userPage.setCurrent(current - 1);
                adapter(userPage);
            }
        });
    }

    /**
     * 分页总和方法
     * 填写分页总和标签
     *
     * @param searchHBox 分页盒子
     * @param sumStrip   总页数
     */
    private void pagingSum(HBox searchHBox, Long sumStrip) {

        Label explainLab = new Label("总记录数");
        searchHBox.getChildren().add(explainLab);

        Label sumLab = new Label(String.valueOf(sumStrip));
        searchHBox.getChildren().add(sumLab);
        //样式
        LabStyle.status(sumLab, LabStyle.STOP);

        Label unitLab = new Label("条");
        searchHBox.getChildren().add(unitLab);

    }

    /**
     * 适配方法
     * 调取对应请求分页界面
     *
     * @param page 分页模型
     */
    private void adapter(Page<T> page) {

        String name = type.getName();

        switch (name) {
            case "com.bt.lms.entity.User":
                NetWorkUtils.reqUserPagingPage(pageSkipInfo, (Page<User>) page);
                break;
            case "com.bt.lms.entity.Role":
                NetWorkUtils.reqRolePagingPage(pageSkipInfo, (Page<Role>) page);
                break;
            case "com.bt.lms.entity.StudyCen":
                NetWorkUtils.reqStudyCenPagingPage(pageSkipInfo, (Page<StudyCen>) page);
                break;
            case "com.bt.lms.entity.Major":
                NetWorkUtils.reqMajorPagingPage(pageSkipInfo, (Page<Major>) page);
                break;
        }
    }

}
